package com.apartment.common.handler;


import com.apartment.common.core.domain.model.LoginUser;
import com.apartment.common.utils.SecurityUtils;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;

import org.apache.ibatis.reflection.MetaObject;

import org.springframework.stereotype.Service;

import javax.sql.DataSource;
import java.time.LocalDateTime;
import java.util.Date;


@Service
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        LoginUser user = SecurityUtils.getLoginUser();
        this.setFieldValByName("createBy",user.getUsername(),metaObject);
        this.setFieldValByName("updateBy",user.getUsername(),metaObject);
        this.setFieldValByName("createTime",new Date(),metaObject);
        this.setFieldValByName("updateTime",new Date(),metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
        LoginUser user = SecurityUtils.getLoginUser();
        this.setFieldValByName("updateBy",user.getUsername(),metaObject);
    }
}